package com.yiqi.zhiyuan.feature.certification.presenter;

import com.yiqi.zhiyuan.entity.vo.BrandChildVO;
import com.yiqi.zhiyuan.entity.vo.CarBrandVO;
import com.yiqi.zhiyuan.feature.certification.contract.BrandContract;
import com.yiqi.zhiyuan.feature.certification.dependencies.brand.DaggerBrandPresenterComponent;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @ProjectName: ZhiYuan
 * @Description: java类作用描述
 * @Author: lijuwen
 * @CreateDate: 2019/3/14 10:54
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/14 10:54
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class BrandPresenter implements BrandContract.Presenter {

    private BrandContract.View mView;

    @Inject
    BrandContract.Model mModel;


    public BrandPresenter(BrandContract.View mView) {
        this.mView = mView;
        DaggerBrandPresenterComponent.create().inject(this);
    }


    @Override
    public void start() {

    }


    @Override
    public void doQueryCarBrands() {
        mModel.doQueryCarBrands()
                .compose(mView.bindToLife())
                .subscribe(new Observer<List<CarBrandVO>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(List<CarBrandVO> carBrandVOS) {
                        mView.dismissLoading();
                        mView.showQueryCarBrandsSuccess(carBrandVOS);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void doQueryCarBrandModel(String bcode) {
        mModel.doQueryCarBrandModel(bcode)
                .compose(mView.bindToLife())
                .subscribe(new Observer<List<BrandChildVO>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(List<BrandChildVO> carBrandVOS) {
                        mView.dismissLoading();
                        mView.showQueryCarBrandModelSuccess(carBrandVOS);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }
}
